<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        position: absolute;
        left: 0;
        right: 0;
        background-color: orange;
        width: 100px;
        height: 100px;
        border-radius: 50%;
      }
    </style>
  </head>
  <body>
    <div id="demo"></div>
    <script>
      const demoEl = document.querySelector("#demo");
      demoEl.onclick = function () {
        startMove(this);
      };

      function startMove(domEl) {
        clearInterval(domEl.timer);
        let iSpeedX = 6;
        let iSpeedY = 8;
        const u = 0.8;
        const g = 3;
        domEl.timer = setInterval(() => {
          iSpeedY += g;
          let newTop = domEl.offsetTop + iSpeedY;
          let newLeft = domEl.offsetLeft + iSpeedX;
          let maxTop =
            document.documentElement.clientHeight - domEl.clientHeight;
          let maxLeft =
            document.documentElement.clientWidth - demoEl.clientWidth;
          if (newTop >= maxTop) {
            iSpeedY *= u;
            iSpeedX *= u;
            iSpeedY *= -1;
            newTop = maxTop;
          }

          if (newTop <= 0) {
            iSpeedY *= u;
            iSpeedX *= u;
            iSpeedY *= -1;
            newTop = 0;
          }

          if (newLeft >= maxLeft) {
            iSpeedY *= u;
            iSpeedX *= u;
            iSpeedX *= -1;
            newLeft = maxLeft;
          }

          if (newLeft <= 0) {
            iSpeedY *= u;
            iSpeedX *= u;
            iSpeedX *= -1;
            newLeft = 0;
          }

          if (Math.abs(iSpeedX) <= 1) {
            iSpeedX = 0;
          }

          if (Math.abs(iSpeedY) <= 1) {
            iSpeedY = 1;
          }

          if (
            iSpeedX === 0 &&
            iSpeedY === 0 &&
            newTop === maxTop &&
            newLeft === maxLeft
          ) {
            clearInterval(domEl.timer);
          } else {
            domEl.style.top = newTop + "px";
            domEl.style.left = newLeft + "px";
          }
        }, 30);
      }
    </script>
  </body>
</html>
